<template>
  <div>
    <el-container>
      <el-header>
        <span>个人中心</span>
      </el-header>
    </el-container>
    <div class="graduate-info">
      <el-descriptions title="描述信息">
        <template slot="extra">
          <el-button
            type="primary"
            size="small"
            class="update-button"
            @click="dialogFormVisible = true"
            >更新</el-button
          >
        </template>
        <el-descriptions-item label="我的头像">
          <el-upload
            class="avatar-uploader"
            :action="actionUrl"
            :show-file-list="false"
            :on-change="handleChange"
            multiple
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-descriptions-item>
        <el-descriptions-item label="用户名称">{{
          graduateInfoForm.gname
        }}</el-descriptions-item>
        <el-descriptions-item label="你的昵称">{{
          graduateInfoForm.gnickname
        }}</el-descriptions-item>
        <el-descriptions-item label="你的性别">{{
          graduateInfoForm.gsex
        }}</el-descriptions-item>
        <el-descriptions-item label="你的年龄">{{
          graduateInfoForm.gage
        }}</el-descriptions-item>
        <el-descriptions-item label="出生日期">{{
          graduateInfoForm.gbirthday
        }}</el-descriptions-item>
        <el-descriptions-item label="手机号码">{{
          graduateInfoForm.gphone
        }}</el-descriptions-item>
        <el-descriptions-item label="学校名称">{{
          graduateInfoForm.gschoolname
        }}</el-descriptions-item>
        <el-descriptions-item label="邮箱地址">{{
          graduateInfoForm.email
        }}</el-descriptions-item>
      </el-descriptions>
      <!-- 更新对话框 -->
      <el-dialog
        title="更新"
        :visible.sync="dialogFormVisible"
        modal
        :append-to-body="true"
      >
        <el-form :model="graduateInfoForm" :inline="true">
          <el-form-item label="用户名称" :label-width="formLabelWidth">
            <el-input
              v-model="graduateInfoForm.gname"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户昵称" :label-width="formLabelWidth">
            <el-input
              v-model="graduateInfoForm.gnickname"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-input
              v-model="graduateInfoForm.gsex"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-input
              v-model="graduateInfoForm.gage"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="学校" :label-width="formLabelWidth">
            <el-input
              v-model="graduateInfoForm.gschoolname"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="出生日期" :label-width="formLabelWidth">
            <el-input
              v-model="graduateInfoForm.gbirthday"
              autocomplete="off"
              placeholder="请输入1999-01-01的格式"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号码" :label-width="formLabelWidth">
            <el-input
              v-model="graduateInfoForm.gphone"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱地址" :label-width="formLabelWidth">
            <el-input
              v-model="graduateInfoForm.email"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <el-container>
      <el-footer>
        <div>
          <span>专注于互联网校招</span>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "RecommendGraduateinfo",

  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: "120px",
      graduateInfoForm: {
        gid: 0,
        gname: "",
        gnickname: "",
        gpassword: "",
        gsex: "",
        gage: 0,
        gbirthday: "",
        gphone: "",
        gschoolname: "",
        email: "",
      },
      actionUrl: "http://127.0.0.1:8088/graduate/upload",
      imageUrl: "",
      saveImageUrl: {
        imgUrl: "",
      },
    };
  },

  mounted() {
    this.loadData();
    this.loadHeadImg();
  },

  watch: {},

  methods: {
    loadData() {
      this.$axios
        .post("/graduate/queryByGid")
        .then((res) => {
          if (res.data.result != null) {
            this.graduateInfoForm = res.data.result;
          } else {
            return;
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },
    // 更新
    update() {
      this.$axios
        .post("/graduate/updateGraduateInfo", this.graduateInfoForm)
        .then((res) => {
          var response = res.data.desc;
          if (response === "更新失败") {
            this.$message({
              message: "更新失败",
              type: "error",
            });
          } else if (response === "更新成功") {
            this.$message({
              message: "更新成功",
              type: "success",
            });
            // 关闭对话框
            this.dialogFormVisible = false;
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },
    // 加载头像
    loadHeadImg() {
      this.$axios
        .post("/graduate/loadHeadImage")
        .then((res) => {
          if (res.data.desc != "") {
            this.imageUrl = res.data.desc;
          } else {
            return;
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },

    handleChange(file) {
      if (file.response === "") {
        return;
      }
      this.imageUrl = file.response;
      this.saveImageUrl.imgUrl = this.imageUrl;
      this.$axios
        .post("/graduate/save", this.saveImageUrl)
        .then((res) => {
          var response = res.data.desc;
          if (response === "保存失败") {
            return;
          } else if (response === "保存成功") {
            this.$message({
              message: "更新头像成功",
              type: "success",
            });
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },
  },
};
</script>

<style scoped>
.el-header {
  background-color: #409eff;
  color: white;
  line-height: 60px;
}

.el-footer {
  background-color: #409eff;
  color: white;
  line-height: 60px;
}

.el-footer div {
  margin-left: 43%;
}

.graduate-info {
  margin-bottom: 10px;
  height: 480px;
  background: whitesmoke;
  /* background-image: url(https://img2.baidu.com/it/u=2699034911,1369297825&fm=253&fmt=auto&app=138&f=JPEG?w=890&h=500);
    background-size: 1355px 480px; */
}

.el-descriptions {
  margin-top: 10px;
  margin-left: 20px;
  margin-right: 20px;
}

.update-button {
  margin-top: 8px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
